為什麼要設置本機SSH,可以模擬虛擬機是一台伺服器,像我其實我不是很喜歡一直在虛擬機下指令,所以我會選擇用本機的終端機做SSH去連線虛擬機。
10.0.2.15,這是虛擬機(Centos 7) 的IP位址。
[root@localhost ~]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:4a:ca:c6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic enp0s3
valid_lft 83273sec preferred_lft 83273sec
inet6 fe80::aafe:fe7a:eeb5:d621/64 scope link noprefixroute
valid_lft forever preferred_lft forever
點選設定 > 網路
附加到「NAT」 > 點選進階 > 連接埠轉送
選擇旁邊綠色的「+」符號
※ 主機連接埠設定為自定義埠,即1024之後的埠。
設定完以後 啟動虛擬機(CentOS)。
安裝 ssh 套件。
[root@localhost ~]# yum -y install openssh*
確認 openssh
套件已有安裝。
[root@localhost ~]# yum list installed | grep openssh
確認 ssh
服務是否啟用,有 sshd 的服務,就代表已啟用。
[root@localhost ~]# ps -e |grep ssh
1631 ? 00:00:00 sshd
1701 ? 00:00:00 sshd
※ 如果沒有啟用,請輸入以下指令,之後再回頭確認是否啟用。
[root@localhost ~]# /etc/init.d/ssh start
查看 ssh
服務狀態。
[root@localhost ~]# service sshd status
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since 六 2020-10-03 00:45:23 CST; 50min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 1631 (sshd)
CGroup: /system.slice/sshd.service
└─1631 /usr/sbin/sshd -D
10月 03 00:45:23 localhost.localdomain systemd[1]: Starting OpenSSH server d...
10月 03 00:45:23 localhost.localdomain sshd[1631]: Server listening on 0.0.0...
10月 03 00:45:23 localhost.localdomain sshd[1631]: Server listening on :: po...
10月 03 00:45:23 localhost.localdomain systemd[1]: Started OpenSSH server da...
10月 03 00:49:05 localhost.localdomain sshd[1694]: Connection closed by 10.0...
10月 03 00:53:00 localhost.localdomain sshd[1697]: Connection closed by 10.0...
10月 03 00:53:10 localhost.localdomain sshd[1699]: Connection closed by 10.0...
10月 03 00:53:27 localhost.localdomain sshd[1701]: Accepted password for roo...
Hint: Some lines were ellipsized, use -l to show in full.
我使用的是 maxOS ,有些人可能想說,我前面不是以Windows 系統操作 VirualBox 嗎?因為中秋節連假關係,我只帶筆電回家...
開啟筆電的終端機,若 Windows 沒終端機,可以嘗試使用 Bitvise SSH 去連線看看。
使用ssh
指令連線,-p
代表是 port 埠位,以 root
帳號連線 @ 接著 閘口IP。
sian@MacBook-Pro ~ % ssh -p 8000 root@127.0.0.1
The authenticity of host '[127.0.0.1]:8000 ([127.0.0.1]:8000)' can't be established.
ECDSA key fingerprint is SHA256:X6s5HCvRRJdE/IYH9SP8BYZBvsa3KPJjJkiwIRn4qz8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:8000' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
Last login: Fri Oct 3 00:53:27 2020 from gateway
[root@localhost ~]#